package com.galleryviewpager.m_qq;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class MessageRemoveView extends View {

    private Paint mPaint;
    private int mWidth, mHeight;

    private Path path = new Path();
    private float mMoveCircleY = 200, mMoveCircleX;
    private float mCircleX = 300, mCircleY = 200;

    private float lastY, firstY;
    private boolean isUp;
    private float mCircleRadius = 30;
    private int mMoveCircleRadius;

    private boolean isCanDraw = true;


    public MessageRemoveView(Context context) {
        this(context, null);
    }

    public MessageRemoveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MessageRemoveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

   
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        mWidth = widthMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(widthMeasureSpec) : 600;

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        mHeight = heightMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(heightMeasureSpec) : 600;
        setMeasuredDimension(mWidth, mHeight);

        mMoveCircleX = mWidth / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.RED);
        canvas.drawCircle(mMoveCircleX, firstY, mCircleRadius--, mPaint);
        canvas.drawCircle(mMoveCircleX, lastY, 30f, mPaint);

        path.reset();
        path.moveTo(mMoveCircleX - 30, firstY);
        path.quadTo(mMoveCircleX, (lastY - firstY) / 2, mMoveCircleX - 30, lastY);

        path.lineTo(mMoveCircleX + 30, lastY);

        path.quadTo(mMoveCircleX, (lastY - firstY) / 2, mMoveCircleX + 30, firstY);
        path.lineTo(mMoveCircleX - 30, firstY);
        path.close();


        mPaint.setColor(Color.GREEN);
        canvas.drawPath(path, mPaint);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                firstY = event.getY();
                mCircleRadius = 30;
                break;
            case MotionEvent.ACTION_MOVE:
                mMoveCircleY = event.getY();
                isUp = !(mMoveCircleY < lastY);
                lastY = mMoveCircleY;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                if ((mMoveCircleY - mMoveCircleY) > mMoveCircleRadius * 3) {
                    isCanDraw = false;
                    invalidate();
                }
                break;
        }
        return true;
    }
}
